package com.yqg.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.education.common.Result;
import com.yqg.education.entity.StuFeedback;
import com.yqg.education.entity.StuHonor;
import com.yqg.education.mapper.StuHonorMapper;
import com.yqg.education.service.IStuHonorService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * StuHonorController 前端控制器
 *
 * @author zxf
 * @since 2025-04-15
 */
@RestController
@RequestMapping("/stu-honor")
public class StuHonorController {

    @Resource
    private IStuHonorService stuHonorService;

    @Resource
    private StuHonorMapper stuHonorMapper;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    public Result save(@RequestBody StuHonor stuHonor) {
        stuHonor.setCreateTime(LocalDateTime.now());
        return Result.success(stuHonorService.saveOrUpdate(stuHonor));
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        StuHonor stuHonor = stuHonorService.getById(id);
        stuHonor.setIsDelete(1);
        return Result.success(stuHonorService.saveOrUpdate(stuHonor));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<StuHonor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        List<StuHonor> list = stuHonorService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(stuHonorService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           String name,
                           @RequestParam(required = false) Integer stuId) {
        pageNum = pageNum * pageSize - pageSize;
        Map<String,Object> map = new HashMap<>();
        List<StuHonor> stuHonorList = stuHonorMapper.findPage(pageNum, pageSize, name, stuId);
        Integer count = stuHonorMapper.findCount(name, stuId);
        map.put("records",stuHonorList);
        map.put("total", count);
        return Result.success(map);
    }

}

